package com.rfid.def;

public class MIFARE_CLASSIC {
    public static int UID_LEN = 4;
    public static int BYTES_P_BLOCK = 16;
    public static int KEY_LEN = 6;
    public static int SMALL_SECTORS = 32;
    public static int LARGE_SECTORS = 8;
    public static int BLOCKS_P_SMALL_SECTOR = 4;
    public static int BLOCKS_P_LARGE_SECTOR = 16;
    public static char CMD_AUTHEN = '\001';
    public static char CMD_READ = '\002';
    public static char CMD_WRITE = '\003';
    public static char CMD_INCREMENT = '\004';
    public static char CMD_DECREMENT = '\005';
    public static char CMD_RESTORE = '\006';
    public static char CMD_TRANSFER = '\007';
    public static char CMD_FORMATVALUEBLOCK = '\b';
    public static char CMD_READ_SECTORBLKS = '\t';
    public static char CMD_WRITE_SECTORBLKS = '\n';
    public static char CMD_READMULTIPLE = '\013';
    public static char CMD_WRITEMULTIPLE = '\f';

    public static void create_value_block(byte blkAddr, long value, byte[] pBlock) {
        long pValue = value & 0xFFFFFFFFFFFFFFFFL;
        pBlock[0] = (byte) (int) (pValue & 0xFFL);
        pBlock[1] = (byte) (int) (pValue >> 8 & 0xFFL);
        pBlock[2] = (byte) (int) (pValue >> 16 & 0xFFL);
        pBlock[3] = (byte) (int) (pValue >> 24 & 0xFFL);
        pBlock[4] = (byte) (pBlock[0] ^ 0xFFFFFFFF);
        pBlock[5] = (byte) (pBlock[1] ^ 0xFFFFFFFF);
        pBlock[6] = (byte) (pBlock[2] ^ 0xFFFFFFFF);
        pBlock[7] = (byte) (pBlock[3] ^ 0xFFFFFFFF);
        pBlock[8] = pBlock[0];
        pBlock[9] = pBlock[1];
        pBlock[10] = pBlock[2];
        pBlock[11] = pBlock[3];
        pBlock[12] = blkAddr;
        pBlock[13] = (byte) (blkAddr ^ 0xFFFFFFFF);
        pBlock[14] = blkAddr;
        pBlock[15] = (byte) (blkAddr ^ 0xFFFFFFFF);
    }

    public static boolean check_value_block_format(byte[] pBlock) {
        if (pBlock[0] != pBlock[8] || pBlock[1] != pBlock[9] || pBlock[2] != pBlock[10] || pBlock[3] != pBlock[11] || pBlock[4] != (pBlock[0] ^ 0xFF) || pBlock[5] != (pBlock[1] ^ 0xFF) || pBlock[6] != (pBlock[2] ^ 0xFF) || pBlock[7] != (pBlock[3] ^ 0xFF) || pBlock[12] != pBlock[14] || pBlock[13] != pBlock[15] || pBlock[12] != (pBlock[13] ^ 0xFF))
            return false;
        return true;
    }
}
